import pymysql
# from dataUtil import kafkaConnect
import json
from multiprocessing import Process
import dataUtil.sparkConnect as spark
from xpinyin import Pinyin
import chardet


def trans_a_example():
    conn = pymysql.connect(
        host='39.108.223.126',
        port=23760,
        user='nj_mortal',
        password='Mortal-1234',
        database='gxfp',
        charset='utf8'
    )
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    sql = 'select * from beihaizhongdian;'
    cursor.execute(sql)
    pro = kafkaConnect.get_producer("test")
    count = 0
    for i in range(4):
        row = cursor.fetchone()
        if not row:
            print(count)
            print("error")
            return
        count = count + 1
        print(row)
        # kafkaConnect.produce_e(pro, bytes(json.dumps(row), 'utf8'))


def consumer_func(msg):
    dict = json.loads(msg, encoding='utf8')
    print(dict)


def temp_trans():
    conn = pymysql.connect(
        host='39.108.223.126',
        port=23760,
        user='nj_mortal',
        password='Mortal-1234',
        database='gxfp',
        charset='utf8'
    )
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    sql = 'select * from beihaizhongdian;'
    cursor.execute(sql)
    row = cursor.fetchone()
    p = Pinyin()
    # spark.create_item_table("antiPoor", result)
    count = 0
    while True:
        row = cursor.fetchone()
        if not row:
            print(count)
            print("error")
            return
        count = count + 1
        print(row)
        result = []
        for i in list(row.values()):
            pingyin = p.get_pinyin(i, splitter="_")
            pingyin = pingyin.split(" ")[0]
            pingyin = pingyin.split("（")[0]
            if pingyin == "" or not (str.isalpha(pingyin[-1]) or str.isdigit(pingyin[-1])):
                pingyin = "null"
            result.append(pingyin)
        spark.insert_item_table("antiPoor", result)


def temp_trans2():
    conn = pymysql.connect(
        host='39.108.223.126',
        port=23760,
        user='nj_mortal',
        password='Mortal-1234',
        database='gxfp',
        charset='utf8'
    )
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    # sql = 'select * from beihaizhongdian;'
    sql = "select COLUMN_NAME from information_schema.COLUMNS where table_name = 'beihaizhongdian';"
    cursor.execute(sql)
    re = cursor.fetchall()
    print(re)
    result = []
    p = Pinyin()
    for row in re:
        for i in list(row.values()):
            pingyin = p.get_pinyin(i, splitter="_")
            pingyin = pingyin.split(" ")[0]
            pingyin = pingyin.split("（")[0]
            pingyin = pingyin.strip(' ')
            result.append(pingyin)
    spark.create_item_table("antipoor2", result)
    print(result)
    return

    p = Pinyin()

    count = 0
    while True:
        row = cursor.fetchone()
        if not row:
            print(count)
            print("error")
            return
        count = count + 1
        print(row)
        result = []
        for i in list(row.values()):
            pingyin = p.get_pinyin(i, splitter="_")
            pingyin = pingyin.split(" ")[0]
            pingyin = pingyin.split("（")[0]
            if pingyin == "" or not (str.isalpha(pingyin[-1]) or str.isdigit(pingyin[-1])):
                pingyin = "null"
            result.append(pingyin)
        try:
            spark.insert_item_table("antipoor", result)
        except:
            continue


if __name__ == "__main__":
    # spark.create_item_table("temp", ['a', 'b'])
    temp_trans2()